package com.paireach.bw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.blankj.utilcode.utils.NetworkUtils;
import com.paireach.bw.R;
import com.paireach.bw.activity.BackActivity;
import com.paireach.bw.adapter.BackOrderAdapter;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.base.BaseFragment;
import com.paireach.bw.bean.LocationInfo;
import com.paireach.bw.bean.OrderResult;
import com.paireach.bw.bean.OrdersBean;
import com.paireach.bw.bean.ReturnInfo;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.utils.BDTraceUtil;
import com.paireach.bw.utils.GsonUtil;
import com.paireach.bw.utils.LocationService;
import com.paireach.bw.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by ${cqc} on 2017/6/19.
 */

public class BackFrag212 extends BaseFragment {

    private View rootView;
    private Button btn;
    private Toolbar toolbar;
    private TextView tvTip;
    private RecyclerView recyclerView;
    private List<OrdersBean> orders;
    private String orderCode;//负载单单号
    private TextView tv_footer;
    private boolean fromNotification;


    public static BackFrag212 getInstance(OrderResult orderResult, boolean fromNotification) {
        BackFrag212 frag212 = new BackFrag212();
        Bundle bundle = new Bundle();
        bundle.putSerializable("BackOrderResult", orderResult);
        bundle.putBoolean("fromNotification", fromNotification);
        frag212.setArguments(bundle);
        return frag212;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        OrderResult orderResult = (OrderResult) bundle.getSerializable("BackOrderResult");
        orders = orderResult.getOrders();
        orderCode = orders.get(0).getORDER_CODE();
        fromNotification = bundle.getBoolean("fromNotification", false);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_back_origin_end, container, false);
        toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        tvTip = (TextView) rootView.findViewById(R.id.tv_tip);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        btn = (Button) rootView.findViewById(R.id.btn);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvTip.setVisibility(View.GONE);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().onBackPressed();
            }
        });
        if (fromNotification) {
            toolbar.setTitle("消息通知");
        } else {
            toolbar.setTitle("运单未接收");
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
//        recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        BackOrderAdapter adapter = new BackOrderAdapter(orders);
        recyclerView.setAdapter(adapter);

        View footView = LayoutInflater.from(context).inflate(R.layout.footer_back, recyclerView, false);
        adapter.setFooterView(footView);
        tv_footer = (TextView) footView.findViewById(R.id.tv_footer);
        tv_footer.setText("确认接单");
        tv_footer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetworkUtils.isConnected()) {
                    showProgressDialog();
                    BDTraceUtil.openTrace();
                    LocationInfo location = ((MyApplication) getActivity().getApplication()).locationInfo;
                    new WayBillRequest().driverAcceptOrder(phoneNumber, orderCode, "", location.getLongitude() + "", location.getLatitude() + "", driverNum);
                } else {
                    showToast("网络连接不可用，请稍后重试");
                }
            }
        });

    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        dismissProgressDialog();
        if (event.getRequestTag() == RequestTag.DRIVER_ACCEPT_ORDER) {
            ReturnInfo info = GsonUtil.jsonToBean(event.getJson(), ReturnInfo.class);
            showToast(info.getMsg());
            if (info.getStatus() == 1) {
                AppEvent appEvent = new AppEvent();
                appEvent.setTag("GetOrderCodeDetail");//通知BackActivity页面请求数据，然后更新fragment
                EventBus.getDefault().post(appEvent);
                LogUtil.d("info.getStatus() == 1");
            }
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        dismissProgressDialog();
        showToast(event.getErrorMessage());
        if (event.getRequestTag() == RequestTag.DRIVER_ACCEPT_ORDER) {
            AppEvent appEvent = new AppEvent();
            appEvent.setTag("加载失败");
            EventBus.getDefault().post(appEvent);
        }
    }
}
